package com.lcz.plugina;

import android.Manifest;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.ryg.dynamicload.DLBasePluginActivity;


public class PluginActivity extends DLBasePluginActivity {

    private static final String TAG = "Client-MainActivity";
    private ServiceConnection mConnecton;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(savedInstanceState);
        findViewById(R.id.btnGetPermission).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String[] permissions = {Manifest.permission.SEND_SMS,};
                if (lackPermissions(permissions)) {
                    getPermission(permissions);
                }else {
                    Toast.makeText(that,"当前已有权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
        findViewById(R.id.btnSendSMSDefault).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DefaultSMSUtil.sendSMSJump(that,"18501231486","default");

            }
        });
        findViewById(R.id.btnSendSMSApi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSMS("18501231486","api");

            }
        });
    }
    private void sendSMS(String phone, String content) {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phone, null, content, null, null);
    }
    public boolean lackPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(that, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }
    private void getPermission(String[] permissions) {
        Toast.makeText(that,"插件开始请求权限", Toast.LENGTH_SHORT).show();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            that.requestPermissions(permissions, 1000);
        }
    }

    private void initView(Bundle savedInstanceState) {
        that.setContentView(R.layout.activity_plugin);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult resultCode=" + resultCode);
        if (resultCode == RESULT_FIRST_USER) {
            that.finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
